package com.example.demoredisspringboot.controller;

import com.alibaba.fastjson.JSON;
import com.example.demoredisspringboot.annotation.RateLimiter;
import com.example.demoredisspringboot.dao.TestEntityDao;
import com.example.demoredisspringboot.enums.LimitType;
import com.example.demoredisspringboot.pojo.TestEntity;
import com.example.demoredisspringboot.pojo.User;
import com.example.demoredisspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * <b><code>TestController</code></b>
 * <p>
 * Description
 * <p>
 * <b>Create Time:</b> 2022/1/19 16:24.
 *
 * @author qd.yt
 * @version 0.0.1
 * @since demo-redis-springboot v0.0.1
 */
@RequestMapping("/test")
@RestController
public class TestController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Resource
    UserService userService;

    @Autowired
    TestEntityDao testEntityDao;

    //增加限流测试 参考https://juejin.cn/post/7098684459159355405
    @GetMapping("/hello")
    @RateLimiter(time = 5,count = 3,limitType = LimitType.IP)
    public String hello() {
        return "hello>>>"+new Date();
    }


    @GetMapping(value = "/get")
    public void test(){
        try {
            //创建一个pojo对象
            User u = new User();
            u.setName("张三");
            u.setId("1");
            u.setPassword("123");
            Map<String, Object> map = new HashMap<String, Object>(2);
            //将pojo对象存入map中，这里需要将pojo对象序列化一下
            map.put("key1", JSON.toJSONString(u));
            //这里将Map写入redis数据库
            stringRedisTemplate.opsForHash().putAll("map1", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/get1")
    public void test1(){
        //创建两个pojo对象
        User u = new User();
        u.setName("张三");
        u.setId("1");
        u.setPassword("123");
        User u2 = new User();
        u2.setName("李四");
        u2.setId("2");
        u2.setPassword("123");

        //把两个对象保存到集合中
        List<User> ulist = new ArrayList<>();
        ulist.add(u);
        ulist.add(u2);
        Map<String, Object> map = new HashMap<String, Object>();

        //把list集合存进map中
        map.put("key1", JSON.toJSONString(ulist));

        //将数据写入redis
        stringRedisTemplate.opsForHash().putAll("map1", map);
    }

    @GetMapping(value = "/set1")
    public void set1(){
        stringRedisTemplate.opsForHash().put("map1","key1","map2-2");
    }

    @GetMapping(value = "/list1")
    public String list(){
        List<Object> hashList = stringRedisTemplate.opsForHash().values("map1");
        return JSON.toJSONString(hashList);
    }


    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable String id){
        User user = userService.findById(id);
        return JSON.toJSONString(user);
    }

    @GetMapping("/getList/{count}")
    public String getUser(@PathVariable Integer count){
        List<User> users = userService.findByIds(count);
        return JSON.toJSONString(users);
    }

    @GetMapping(value = "/testHash")
    public String testHash() {
        TestEntity testEntity = new TestEntity();
        testEntity.setPhone("18803838082");
        testEntity.setName("sun");
        testEntity.setTime(60L);
        TestEntity save = testEntityDao.save(testEntity);

        TestEntity testEntity1 = new TestEntity();
        testEntity1.setPhone("18803838081");
        testEntity1.setName("sun");
        testEntity1.setTime(80L);
        testEntityDao.save(testEntity1);
        return JSON.toJSONString(save);
    }

    @GetMapping(value = "/getHash")
    public String getHash() {

        Optional<TestEntity> optionalTestEntity = testEntityDao.findById("18803838082");
        if (optionalTestEntity.isPresent()) {
            TestEntity testEntity = optionalTestEntity.get();
            System.out.println(" - --  -" + testEntity.getName());
            System.out.println(" - --  -" + testEntity.getPhone());
            System.out.println(" - --  -" + testEntity.getTime());
            return JSON.toJSONString(testEntity);

        } else {
            return ("数据信息不存在，或已过期");
        }
    }

    @GetMapping(value = "/getHashByName")
    public String getHashByName() {
        List<TestEntity> optionalTestEntity = testEntityDao.findByName("sun");
        if (optionalTestEntity!=null) {
            optionalTestEntity.forEach(temp->{
                System.out.println(" - --  -" + temp.getName());
                System.out.println(" - --  -" + temp.getPhone());
                System.out.println(" - --  -" + temp.getTime());
            });

            return JSON.toJSONString(optionalTestEntity);
        } else {
            return ("数据信息不存在，或已过期");
        }
    }



}
